<?php
namespace app\admin\controller\station;

use app\admin\model\ChargePileCost;
use app\admin\model\ChargingStation;
use app\admin\model\CityArea;
use app\admin\model\SystemAdmin;
use app\admin\model\SystemMenu;
use app\admin\BaseController;
use app\admin\model\SystemRole;
use think\facade\Db;
use think\facade\Log;
use think\App;
use think\facade\View;
use think\facade\Request;
use app\admin\model\ChargePile as ChargePileModel;

class ChargePile extends BaseController
{
    /**
     * 列表页面
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $stationModel = new ChargingStation();
        $getList =$stationModel->getAllList(['is_delete'=>1],'id,title');
        View::assign('getList', $getList);
        return View::fetch('pile/index');
    }

    /**
     * 获取列表
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function list(){
        [$page,$limit] = $this->getPage();
        $getData = Request::post();
        $chargePileModel = new ChargePileModel();
        $backData = $chargePileModel->getList($getData,$page,$limit);
        $backData['code'] = 0;
        $backData['msg'] = '获取成功';
        return json()->data($backData);
    }

    /**
     * 添加表单
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function addForm(){
        $stationModel = new ChargingStation();
        $billingModelListModel = new ChargePileCost();
        $getList =$stationModel->getAllList(['is_delete'=>1],'id,title');
        $billingModelList =$billingModelListModel->getAllList(['is_status'=>1],'id,title');
        View::assign('getList', $getList);
        View::assign('billingModelList', $billingModelList);
        return View::fetch('pile/add');
    }

    /**
     * 添加数据
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function addData(){
        $getData = Request::post();
        if(!$getData['title']) return $this->fail('请输入充电桩名称');
        if(!$getData['device_type']) return $this->fail('请选择充电桩类型');
        if(!$getData['station_id']) return $this->fail('请选择所属站点');
        if(!$getData['device_sn']) return $this->fail('请输入设备编号');
        if(!$getData['cost_id']) return $this->fail('请选择计费模型');
        $chargePileModel = new ChargePileModel();
        $getInfo = $chargePileModel->getInfoByDeviceSn($getData['device_sn']);
        if($getInfo) return $this->fail('设备编号已经存在');
        $result = $chargePileModel->addData($getData);
        if($result) return $this->success('添加成功');
        return $this->fail('添加失败');
    }

    /**
     * 编辑表单
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function editForm(){
        $id = Request::get('id');
        $chargePileModel = new ChargePileModel();
        $info = $chargePileModel->getInfoById($id);
        $infoStatus = 0;
        if($info) $infoStatus = 1;
        $stationModel = new ChargingStation();
        $billingModelListModel = new ChargePileCost();
        $getList =$stationModel->getAllList(['is_delete'=>1],'id,title');
        $billingModelList =$billingModelListModel->getAllList(['is_status'=>1],'id,title');
        View::assign('getList', $getList);
        View::assign('billingModelList', $billingModelList);
        View::assign('info', $info);
        View::assign('infoStatus', $infoStatus);
        return View::fetch('pile/edit');
    }

    /**
     * 修改信息
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function updateData(){
        $getData = Request::post();
        if(!$getData['title']) return $this->fail('请输入充电桩名称');
        if(!$getData['device_type']) return $this->fail('请选择充电桩类型');
        if(!$getData['station_id']) return $this->fail('请选择所属站点');
        if(!$getData['device_sn']) return $this->fail('请输入设备编号');
        if(!$getData['cost_id']) return $this->fail('请选择计费模型');
        $chargePileModel = new ChargePileModel();
        $getInfo = $chargePileModel->getInfoByDeviceSn($getData['device_sn']);
        if($getInfo && $getInfo['id'] != $getData['id']) return $this->fail('设备编号已经存在');
        $result = $chargePileModel->editData($getData);
        if($result) return $this->success('修改成功');
        return $this->fail('未作任何修改');
    }

    /**
     * 删除信息
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function deleteData(){
        $id = Request::post('id');
        if(!$id) return $this->fail('请选择要删除的充电桩');
        $chargePileModel = new ChargePileModel();
        $info = $chargePileModel->getInfoById($id);
        if(!$info)  return $this->fail('请选择正确的充电桩');
        $result = $chargePileModel->delData($id);
        if($result['code'] == 0) return $this->success('删除成功');
        return $this->fail('删除失败');
    }

}


